package com.web.utils;

import java.lang.reflect.Field;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class Utils {

	/**
	 * md5
	 * 
	 * @param dataStr
	 * @return
	 */
	public static String getMD5(String dataStr) {
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(dataStr.getBytes("UTF8"));
			byte s[] = m.digest();
			String result = "";
			for (int i = 0; i < s.length; i++) {
				result += Integer.toHexString((0x000000FF & s[i]) | 0xFFFFFF00).substring(6);
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 生成固定长度的字符串
	 * 
	 * @param length
	 * @return
	 */
	public static final String ALLCHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZauiopdfgh";
	public static final String LETTERCHAR = "23456789abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

	public static String getRandomString(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 时间戳转换日期
	 * 
	 * @param stamp
	 * @return
	 */
	public static String stampToTime(Long stamp) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(stamp));
	}

	public static String stampToTime(String stamp) {
		return stampToTime(Long.parseLong(stamp));
	}

	public static String stampToTime(Timestamp timestamp) {
		return stampToTime(timestamp.getTime());
	}

	/**
	 * 日期转换为时间戳
	 * 
	 * @param timers
	 * @return
	 */
	public static long timeToStamp(String datetime) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = simpleDateFormat.parse(datetime);
			return date.getTime();
		} catch (ParseException e) {
			return 0;
		}
	}

	public static boolean isWindow() {
		return System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0;
	}

	/**
	 * 转换bean为map
	 */
	public static <T> Map<String, Object> bean2Map(T source){
		Map<String, Object> result = new HashMap<>();
		Class<?> sourceClass = source.getClass();
		Field[] sourceFiled = sourceClass.getDeclaredFields();
		for (Field field : sourceFiled) {
			field.setAccessible(true);
			try {
				result.put(field.getName(), field.get(source));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	/**
	 * map转bean
	 */
	public static <T> T map2Bean(Map<String, Object> source, Class<T> instance) {
		try {
			@SuppressWarnings("deprecation")
			T object = instance.newInstance();
			Field[] fields = object.getClass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				field.set(object, source.get(field.getName()));
			}
			return object;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
